///////////////////////////////////////////////////////////////////////////////
// Project: Hoyo Client Game
//
// hy_server_monster_group_data.cpp
// Created: 2013-7-30
// Author: Like Cheng
///////////////////////////////////////////////////////////////////////////////

#include "hy_server_monster_group_data.h"
#include "hy_common_system/hy_common_func.h"


hy_server_monster_list::hy_server_monster_list()
{
	m_monster_id = 0;	
	m_pos = 0;			
	m_scale = 1.0f;		
	m_add_attr = 1.0f;	
	m_level = 1;

	m_skill_set.clear();
	m_dialog_msg = "";	
}

void hy_server_monster_list::set_server_monster_list( Json::Value &v )
{
	m_value = v;
}

void hy_server_monster_list::parse_data()
{
	m_level = get_json_int( m_value, "level" );
	m_monster_id = get_json_int( m_value, "monster_id" );
	m_pos = get_json_int( m_value, "pos" );
	m_quality = get_json_int( m_value, "color" );

	Json::Value jv = m_value["skill_id"];
	for( unsigned int i = 0u; i < jv.size(); i++ )
	{
		m_skill_set.insert( jv[i].asInt() );
	}
	Json::Value jv2 = m_value["param"];

	m_vec_param.clear();
	for( unsigned int i = 0u; i < jv2.size(); i++ )
	{
		m_vec_param.push_back( (float)jv[i].asDouble() );
	}
}

hy_server_monster_group_data::hy_server_monster_group_data()
{
	init();
}

hy_server_monster_group_data::~hy_server_monster_group_data()
{
	hy_server_monster_list_map::iterator it;
	for( it = m_server_monster_list_map.begin(); it != m_server_monster_list_map.end(); it++ )
	{
		DEL_POINTER( it->second );
	}
}

bool hy_server_monster_group_data::init()
{
	m_monster_group_id = 0;
	m_value.clear();
	m_load_dirty = 0;
	return true;
}

int	hy_server_monster_group_data::get_is_player_num()
{
	return get_json_int( m_value, "is_player_num" );
}

void hy_server_monster_group_data::set_value( Json::Value& v )
{
	m_value = v;
	m_load_dirty = 1;
}

void hy_server_monster_group_data::parse_data()
{
	Json::Value jv = m_value["monster_list"];
	for( uint i = 0u; i < jv.size(); i++ )
	{
		Json::Value cv = jv[i];

		hy_server_monster_list* ml = new hy_server_monster_list;
		ml->set_server_monster_list( cv );
		ml->parse_data();
		int pos = cv["pos"].asInt();

		m_server_monster_list_map.insert( std::make_pair( pos, ml ) );
	}
	
}

hy_server_monster_list* hy_server_monster_group_data::get_server_monster_list( int index )
{
	hy_server_monster_list_map::iterator it = m_server_monster_list_map.find( index );
	if( it != m_server_monster_list_map.end() )
	{
		return it->second;
	}
	return NULL;
}
///////////////////////////////////////////////////////////////////////////////
hy_server_monster_group_mgr* hy_server_monster_group_mgr_instance = NULL;

hy_server_monster_group_mgr::hy_server_monster_group_mgr()
{

}

hy_server_monster_group_mgr::~hy_server_monster_group_mgr()
{

}

hy_server_monster_group_mgr* hy_server_monster_group_mgr::instance()
{
	if( hy_server_monster_group_mgr_instance == NULL )
		hy_server_monster_group_mgr_instance = new hy_server_monster_group_mgr;
	return hy_server_monster_group_mgr_instance;
}

void hy_server_monster_group_mgr::purge_instance()
{
	DEL_POINTER( hy_server_monster_group_mgr_instance );
}

bool hy_server_monster_group_mgr::add_server_monster_group_data( hy_server_monster_group_data* d , bool is_update )
{
	hy_server_monster_group_data_map::iterator it = m_server_monster_group_data_map.find( d->get_id() );
	if( it != m_server_monster_group_data_map.end() )
	{
		if( is_update )
		{
			delete it->second;
			it->second = d;
			return true;
		}
	}
	else
	{
		m_server_monster_group_data_map.insert( std::make_pair( d->get_id(), d ) );
	}
	return true;
}

hy_server_monster_group_data*	hy_server_monster_group_mgr::get_server_monster_group_data( int id )
{
	hy_server_monster_group_data_map::iterator it = m_server_monster_group_data_map.find( id );
	if( it != m_server_monster_group_data_map.end() )
	{
		return it->second;
	}
	return NULL;
}